651D - Image Preview - CodeForces Solution


binary search brute force dp two pointers *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll,ll> pl;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define all(x) x.begin(),x.end()  
#define allr(x) x.rbegin(),x.rend() 
#define print(a) for(auto e:a) cout<<e<<" ";cout<<endl; 
#define cr(_) {cout<<_<<endl;return;}
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define s second  
#define f first
#define pb push_back
#define sz(a) a.size()
const ll N=1e5+5;
const ll MOD=998244353;
using namespace std;

bool isPrime(int num) {
    if (num < 2) {
        return false;
    }
    for (int i = 2; i <= sqrt(num); ++i) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}
int dx[] = {0, 0, 1, -1};
    int dy[] = {1, -1, 0, 0};
void solve(){
ll n,a,b,c;
cin>>n>>a>>b>>c;
string s;
cin>>s;
vl pre(n+1);
rep(i,1,n+1){
    pre[i]=(s[i-1]=='w'?b+1:1);

}
rep(i,1,n+1) pre[i]+=pre[i-1];

ll l=0;
ll r=n;
ll res=0;
while(l<=r){
    ll mid=(l+r)/2;
    ll ok=0;
    rep(i,1,mid+1){
        ll sum=pre[i]+pre[n]-pre[n-mid+i];
        ll sum1=min(2*(i-1)*a+(mid-i)*a,2*(mid-i)*a+(i-1)*a);
        if(sum+sum1<=c){
            ok=1;
        }
    }
    if(ok){
        res=mid;
        l=mid+1;
    }
    else r=mid-1;
    
}
cout<<res<<endl;

}



int main() {
    int t=1;
 //  cin >> t;

    while (t--) {
      solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array